import Vue from 'vue'
import { login,getAction} from "@/api/index"
//用户信息的仓库
//, logout,queryPermissionsByUser

const user = {
  state: {
    token: '',
    username: '',
    avatar: '',
    permissionList: [],
    info: {}
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, { username}) => {
      state.username = username
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_PERMISSIONLIST: (state, permissionList) => {
      state.permissionList = permissionList
    },
    SET_INFO: (state, info) => {
      state.info = info
    },
  },

  actions: {
    // 登录
    Login({ commit }, userInfo) {
      return new Promise((resolve, reject) => {
        login(userInfo).then(response => {
          if(response.code =='200'){
            const result = response.data
            Vue.local.set("ACCESS_TOKEN", result.token, 7 * 24 * 60 * 60 * 1000)
            Vue.local.set("USER_NAME", result.username, 7 * 24 * 60 * 60 * 1000)
            Vue.local.set("USER_INFO",result.info, 7 * 24 * 60 * 60 * 1000)
            // Vue.local.set(UI_CACHE_DB_DICT_DATA, result.sysAllDictItems, 7 * 24 * 60 * 60 * 1000)
            console.log(result.token)
            commit('SET_TOKEN', result.token)
            commit('SET_INFO', userInfo)
            commit('SET_NAME', { username: userInfo.username})
            commit('SET_AVATAR', userInfo.avatar)
            resolve(response)
          }else{
            reject(response)
          }
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 获取用户信息
    GetPermissionList({ commit }) {
      return new Promise((resolve, reject) => {

          getAction("/admin/permission").then((res)=>{
            console.log(res)
          })

          const menuData = [
            {
                "redirect": null,
                "path": "/index",
                "component": "index",
                "route": "1",
                "title": "首页",
                "meta": {
                    "keepAlive": false,
                    "internalOrExternal": false,
                    "icon": "home",
                    "componentName": "Index",
                    "title": "订单处理"
                },
                "name": "订单处理",
                "id": "9502685863ab87f0ad1134142788a385"
            },
            {
              "redirect": null,
              "path": "/inex",
              "component": "sale/sale",
              "route": "1",
              "meta": {
                  "keepAlive": false,
                  "internalOrExternal": false,
                  "icon": "dashboard",
                  "componentName": "Index",
                  "title": "销售额"
              },
              "name": "inex",
              "title": "销售额",
              "id": "3"
          },
          {
            "redirect": null,
            "path": "/infx",
            "component": "sale/buy",
            "route": "1",
            "meta": {
                "keepAlive": false,
                "internalOrExternal": false,
                "icon": "dashboard",
                "componentName": "Index",
                "title": "前台购买"
            },
            "name": "in2ex",
            "title": "前台购买",
            "id": "4"
        },
        {
          "redirect": null,
          "path": "/inwx",
          "component": "sale/manage",
          "route": "1",
          "meta": {
              "keepAlive": false,
              "internalOrExternal": false,
              "icon": "dashboard",
              "componentName": "Index",
              "title": "座位管理"
          },
          "name": "in3ex",
          "title": "座位管理",
          "id": "5"
      },
          ]
          console.log("menuData")
          console.log(menuData)
          if (menuData && menuData.length > 0) {
            //update--begin--autor:qinfeng-----date:20200109------for：JEECG-63 一级菜单的子菜单全部是隐藏路由，则一级菜单不显示------
            let newmenuData=[]
            menuData.forEach((item, index) => {
              if (item["children"]) {
                let hasChildrenMenu = item["children"].filter((i) => {
                  return !i.hidden || i.hidden == false
                })
                if (hasChildrenMenu == null || hasChildrenMenu.length == 0) {
                  item["hidden"] = true
                }
              }
            })
            // menuData=newmenuData   //上面的const要改成let，我靠，居然不报错，我直接震撼一整年我靠
            
            //console.log(" menu show json ", menuData)
            //update--end--autor:qinfeng-----date:20200109------for：JEECG-63 一级菜单的子菜单全部是隐藏路由，则一级菜单不显示------
            commit('SET_PERMISSIONLIST', menuData)
          } else {
            reject('getPermissionList: permissions must be a non-null array !')
          }
          resolve(menuData)
          
       
      })
    },

    // 登出
    Logout({ commit, state }) {
      return new Promise((resolve) => {
        let logoutToken = state.token;
        commit('SET_TOKEN', '')
        commit('SET_PERMISSIONLIST', [])
        Vue.local.remove("ACCESS_TOKEN")
        Vue.local.remove("USER_INFO")
        Vue.local.remove("USER_NAME")
        //console.log('logoutToken: '+ logoutToken)
        // logout(logoutToken).then(() => {
          
        resolve()
        // }).catch(() => {
        //   resolve()
        // })
      })
    },


  }
}

export default user